package com.luguangtao.service.impl;

import com.luguangtao.dao.StudentDao;
import com.luguangtao.entity.Student;
import com.luguangtao.service.StudentService;
import com.mongodb.client.result.DeleteResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @类名: com.luguangtao.service.impl
 * @作者: lgt
 * @创建时间: 2019-12-17 08:46
 * @描述:
 **/
@Service
@Repository
public class StudentServiceImpl implements StudentService {

    //编写一个StudentService,其中注入MongoTemplate
    @Autowired
    MongoTemplate mongoTemplate;
    @Autowired
    StudentDao studentDao;

    //添加
    @Override
    public String addStu(Map<String, Object> map, String collectionName) {
        mongoTemplate.save(map,collectionName);
        return "success";
    }

    //修改
    @Override
    public String updateStu(Query query, Update update, String collectionName) {
        mongoTemplate.updateFirst(query,update,collectionName);
        return "success";
    }

    //删除
    @Override
    public DeleteResult delStu(Query query, String collectionName) {
        DeleteResult remove = mongoTemplate.remove(query, collectionName);
        return remove;
    }

    //根据名称查询
    @Override
    public List<Student> selectObjectByName(String name) {
        List<Student> byNameLike = studentDao.findByNameLike(name);
        return byNameLike;
    }

    //根据年龄查询
    @Override
    public List<Student> selectObjectByAge(Integer age) {
        List<Student> byAgeGreaterThan = studentDao.findByAgeGreaterThan(age);
        return byAgeGreaterThan;
    }
}
